package config;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Config {

	public static final String fname = "src/config/config.xml";
	
	public static String[] getConnectionConfig(){
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = dbf.newDocumentBuilder();
			File file = new File(fname);
			if (file.exists()){
				Document doc = docBuilder.parse(file);
				Element element = doc.getDocumentElement();
				
				NodeList	configNodeList = element.getElementsByTagName("connection");
				Node		configNode 		= configNodeList.item(0);
				Element		configElement	= (Element)configNode;
				NodeList	configDriver	= configElement.getElementsByTagName("driver");
				NodeList	configUrl	= configElement.getElementsByTagName("url");
				NodeList	configDatabase	= configElement.getElementsByTagName("database");
				NodeList	configUsername	= configElement.getElementsByTagName("username");
				NodeList	configPassword	= configElement.getElementsByTagName("password");
				
				String[] result = new String[]{
					configDriver.item(0).getChildNodes().item(0).getNodeValue(),
					configUrl.item(0).getChildNodes().item(0).getNodeValue(),
					configDatabase.item(0).getChildNodes().item(0).getNodeValue(),
					configUsername.item(0).getChildNodes().item(0).getNodeValue(),
					configPassword.item(0).getChildNodes().item(0).getNodeValue(),
				};
				
				return result;
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return new String[]{
				"com.mysql.jdbc.Driver",
				"jdbc:mysql://localhost/",
				"khach_san",
				"root",
				"root",
			};
	}
}
